package com.hhh.springai_test.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hhh.springai_test.common.BaseResponse;
import com.hhh.springai_test.common.ErrorCode;
import com.hhh.springai_test.common.ResultUtils;
import com.hhh.springai_test.constant.UserConstant;
import com.hhh.springai_test.exception.BusinessException;
import com.hhh.springai_test.model.dto.bookCategory.AddBookCategoryDTO;
import com.hhh.springai_test.model.dto.bookCategory.UpdateBookCategoryDTO;
import com.hhh.springai_test.model.po.BookCategory;
import com.hhh.springai_test.service.BookCategoryService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bookCategory")
public class BookCategoryController {

    @Resource
    private BookCategoryService bookCategoryService;

    @PostMapping("/add")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addBookCategory(@RequestBody AddBookCategoryDTO addBookCategoryDTO) {
        if (addBookCategoryDTO == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        if (StringUtils.isBlank(addBookCategoryDTO.getCategorycode()) 
            || StringUtils.isBlank(addBookCategoryDTO.getCategorynamezh())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类编码或名称不能为空");
        }
        QueryWrapper<BookCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("categorycode", addBookCategoryDTO.getCategorycode());
        long count = bookCategoryService.count(queryWrapper);
        if (count > 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类编码已存在");
        }
        BookCategory bookCategory = new BookCategory();
        BeanUtils.copyProperties(addBookCategoryDTO, bookCategory);
        boolean result = bookCategoryService.save(bookCategory);
        if (!result) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "添加失败");
        }
        return ResultUtils.success(bookCategory.getId());
    }

    @DeleteMapping("/delete/{id}")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteBookCategory(@PathVariable("id") Long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id不合法");
        }
        QueryWrapper<BookCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        long count = bookCategoryService.count(queryWrapper);
        if (count == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类不存在");
        }
        boolean result = bookCategoryService.removeById(id);
        return ResultUtils.success(result);
    }

    @PutMapping("/update")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateBookCategory(@RequestBody UpdateBookCategoryDTO updateBookCategoryDTO) {
        if (updateBookCategoryDTO == null || updateBookCategoryDTO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }
        if (StringUtils.isBlank(updateBookCategoryDTO.getCategorycode())
            || StringUtils.isBlank(updateBookCategoryDTO.getCategorynamezh())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类编码或名称不能为空");
        }
        QueryWrapper<BookCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", updateBookCategoryDTO.getId());
        long count = bookCategoryService.count(queryWrapper);
        if (count == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类不存在");
        }
        BookCategory bookCategory = new BookCategory();
        BeanUtils.copyProperties(updateBookCategoryDTO, bookCategory);
        boolean result = bookCategoryService.updateById(bookCategory);
        return ResultUtils.success(result);
    }

    @GetMapping("/get/{id}")
    public BaseResponse<BookCategory> getBookCategoryById(@PathVariable("id") Long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "id不合法");
        }
        QueryWrapper<BookCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        long count = bookCategoryService.count(queryWrapper);
        if (count == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "分类不存在");
        }
        BookCategory bookCategory = bookCategoryService.getById(id);
        return ResultUtils.success(bookCategory);
    }

    @GetMapping("/list")
    public BaseResponse<List<BookCategory>> listBookCategories() {
        List<BookCategory> list = bookCategoryService.list();
        return ResultUtils.success(list);
    }
}
